<template>
  <div class="login">
    <van-nav-bar>
      <template #left> <i class="iconfont iconbtn_nav_back"></i> </template>
    </van-nav-bar>

    <div class="main">
      <h2>你好,请登录</h2>

      <van-form ref="form">
        <van-field
          v-model="use.phone"
          name="mobile"
          placeholder="请输入手机号码"
          :rules="rules.phone"
        >
          <template #left-icon>
            <i class="iconfont iconbianzu1"></i>
          </template>
        </van-field>
        <van-field
          v-model="use.code"
          placeholder="请输入验证码"
          :rules="rules.code"
          left-icon="iconfont iconyanzhengma"
        >
          <template #left-icon>
            <i
              class="iconfont iconyanzhengma
"
            ></i>
          </template>
          <template #button>
            <span
              class="mycolor"
              size="small"
              type="primary"
              @click="getCode"
              >{{ isback ? time + 'S后重新发送' : '获取验证码' }}</span
            >
          </template>
        </van-field>

        <p class="word">
          登录既同意<span class="mycolor">《用户使用协议》</span>
          <span class="mycolor">《隐私协议》 </span>
        </p>
        <div style="margin: 16px;" class="submit">
          <van-button
            round
            block
            type="danger"
            native-type="submit"
            @click="submit"
          >
            确定
          </van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
// 验证码
import { apiGetCode } from '@/api/login'

export default {
  data () {
    return {
      isback: false, // 是否倒计时
      time: 5, // 时长
      timer: null, // 计时器
      use: {
        phone: '13588889999',
        code: ''
      },
      rules: {
        phone: [
          { required: true, message: '请输入手机号码', trigger: 'onChang' },
          {
            pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '请输入正确的手机号码'
          }
        ],
        code: [
          { required: true, message: '请输入验证码' },
          {
            pattern: /^\d{4}$/,
            message: '请输入4位验证码',
            trigger: 'onBlur'
          }
        ]
      }
    }
  },
  methods: {
    // 获取验证码
    async getCode () {
      if (this.isback === true) {
        return
      }

      this.isback = true
      const timer = setInterval(() => {
        this.time--
        if (this.time < 0) {
          clearInterval(timer)
          this.isback = false
          this.time = 0
        }
      }, 1000)
      // 校验手机号
      await this.$refs.form.validate('mobile')
      // 添加加载动画
      this.$toast.loading({
        duration: 0,
        message: '加载中',
        forbidClick: false
      })
      const resCode = await apiGetCode(this.use.phone)
      console.log(resCode)
      this.$toast.success(resCode.data)
    },
    //  登录
    submit () {
      alert(234)
    }
  }
}
</script>

<style lang="less" scoped>
html,
body {
  height: 100%;
}
.login {
  height: 100%;
  font-size: 12px;
  .iconbtn_nav_back {
    font-size: 44px;
  }
  /deep/ .van-nav-bar__left {
    padding-left: 0px;
  }
  .main {
    padding: 0 15px;
    h2 {
      margin: 50px 0;
    }
  }
  .mycolor {
    color: #16bed8;
  }
  .word {
    margin: 15px 0 12px 0;
  }
}
</style>
